package com.party.business.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.party.business.domain.*;
import com.party.business.mapper.TKsMapper;
import com.party.business.service.*;
import com.party.common.core.domain.entity.SysUser;
import com.party.common.utils.DateUtils;
import com.party.common.utils.SecurityUtils;
import com.party.common.utils.StringUtils;
import com.party.system.service.ISysPostService;
import com.party.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 试卷Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-09
 */
@Service
public class TKsServiceImpl extends ServiceImpl<TKsMapper, TKs> implements ITKsService {
    @Autowired
    private TKsMapper tKsMapper;

    @Autowired
    private ITKsQuestionService ksQuestionService;

    @Autowired
    private ITBusinessPostService businessPostService;

    @Autowired
    private ITBusinessDeptService businessDeptService;


    @Autowired
    private ITQuestionOptionService questionOptionService;

    /**
     * 查询试卷
     *
     * @param id 试卷主键
     * @return 试卷
     */
    @Override
    public TKs selectTKsById(String id) {
        TKs  kcdetail=new TKs();
        kcdetail.setCurrentUserId(SecurityUtils.getUserId());
        kcdetail.setId(id);
        TKs tKs = tKsMapper.selectTKsByIdApp(kcdetail);
        TKsQuestion question = new TKsQuestion();
        question.setKsId(id);
        List<TKsQuestion> tKsQuestions = ksQuestionService.selectTKsQuestionList(question);
        for (int i = 0; i < tKsQuestions.size(); i++) {
            TKsQuestion question1 = tKsQuestions.get(i);
            String quId = question1.getQuId();
            TQuestionOption questionOption = new TQuestionOption();
            questionOption.setQuId(quId);
            List<TQuestionOption> tQuestionOptions = questionOptionService.selectTQuestionOptionList(questionOption);
            tKsQuestions.get(i).setQuestionOptionList(tQuestionOptions);
        }
        // 查询试题
        tKs.setQuList(tKsQuestions);

        TBusinessPost businessPostOLd = new TBusinessPost();
        businessPostOLd.setBusinessId(id);
        List<TBusinessPost> oldpost = businessPostService.selectTBusinessPostList(businessPostOLd);

        // 从 oldpost  过滤出 postId
        List<String> postIds = oldpost.stream().map(TBusinessPost::getPostId).collect(Collectors.toList());
        // List<String> postIds 转成 List<long>
        List<Long> longPostIds = postIds.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());
        TBusinessDept businessDeptOLd = new TBusinessDept();
        businessDeptOLd.setBusinessId(id);
        List<TBusinessDept> olddept = businessDeptService.selectTBusinessDeptList(businessDeptOLd);
        List<String> deptIds = olddept.stream().map(TBusinessDept::getDeptId).collect(Collectors.toList());
        List<Long> longDeptIds = deptIds.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());
        tKs.setPostList(longPostIds);
        tKs.setDeptList(longDeptIds);
        return tKs;
    }


    @Override
    public TKs selectTKsByIdApp(String id) {
        TKs tKs = tKsMapper.selectTKsById(id);


        TBusinessPost businessPostOLd = new TBusinessPost();
        businessPostOLd.setBusinessId(id);
        List<TBusinessPost> oldpost = businessPostService.selectTBusinessPostList(businessPostOLd);

        // 从 oldpost  过滤出 postId
        List<String> postIds = oldpost.stream().map(TBusinessPost::getPostId).collect(Collectors.toList());
        // List<String> postIds 转成 List<long>
        List<Long> longPostIds = postIds.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());
        TBusinessDept businessDeptOLd = new TBusinessDept();
        businessDeptOLd.setBusinessId(id);
        List<TBusinessDept> olddept = businessDeptService.selectTBusinessDeptList(businessDeptOLd);
        List<String> deptIds = olddept.stream().map(TBusinessDept::getDeptId).collect(Collectors.toList());
        List<Long> longDeptIds = deptIds.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());
        tKs.setPostList(longPostIds);
        tKs.setDeptList(longDeptIds);
        return tKs;
    }

    /**
     * 查询试卷列表
     *
     * @param tKs 试卷
     * @return 试卷
     */
    @Override
    public List<TKs> selectTKsList(TKs tKs) {
        return tKsMapper.selectTKsList(tKs);
    }

    @Autowired
    private ISysPostService postService;

    @Autowired
    private ISysUserService  sysUserService;

    @Override
    public List<TKs> selectTKsListApp(TKs tKs) {
        SysUser sysUser = sysUserService.selectUserByIdApp(SecurityUtils.getUserId());
        tKs.setCurrentUserId(SecurityUtils.getUserId());
        // 获取当前部门Id
        Long deptId = sysUser.getDeptId();
        if(deptId==null){
            tKs.setCurrDept("888");
        }else{
            tKs.setCurrDept(deptId + "");
        }
        List<Long> psotIds = postService.selectPostListByUserId(SecurityUtils.getUserId());
        // psotIds转成逗号隔开的字符串
        String postIdsStr = psotIds.stream().map(String::valueOf).collect(Collectors.joining(","));
        if(StringUtils.isEmpty(postIdsStr)){
            tKs.setCurrPost("88888");
        }else{
            tKs.setCurrPost(postIdsStr);
        }

        // 工龄
        long gl = sysUser.getGl();
        tKs.setCurrentGl(gl);
        return tKsMapper.selectTKsListApp(tKs);
    }


    /**
     * 新增试卷
     *
     * @param tKs 试卷
     * @return 结果
     */
    @Override
    public int insertTKs(TKs tKs) {
        tKs.setCreateTime(DateUtils.getNowDate());
        return tKsMapper.insertTKs(tKs);
    }

    /**
     * 修改试卷
     *
     * @param tKs 试卷
     * @return 结果
     */
    @Override
    public int updateTKs(TKs tKs) {
        tKs.setUpdateTime(DateUtils.getNowDate());
        return tKsMapper.updateTKs(tKs);
    }

    /**
     * 批量删除试卷
     *
     * @param ids 需要删除的试卷主键
     * @return 结果
     */
    @Override
    public int deleteTKsByIds(String[] ids) {
        return tKsMapper.deleteTKsByIds(ids);
    }

    /**
     * 删除试卷信息
     *
     * @param id 试卷主键
     * @return 结果
     */
    @Override
    public int deleteTKsById(String id) {
        return tKsMapper.deleteTKsById(id);
    }


    @Override
    public void updatePostAndDept(TKs tKs) throws Exception {
        // 删除之前所有的岗位
        TBusinessPost businessPostOLd = new TBusinessPost();
        businessPostOLd.setBusinessId(tKs.getId());
        List<TBusinessPost> oldpost = businessPostService.selectTBusinessPostList(businessPostOLd);
        for (int i = 0; i < oldpost.size(); i++) {
            TBusinessPost businessPost1 = oldpost.get(i);
            businessPostService.deleteTBusinessPostById(businessPost1.getId());
        }
        List<Long> postList = tKs.getPostList();
        if (StringUtils.isEmpty(postList)) {
            throw new Exception("请选择岗位");
        }
        for (int i = 0; i < postList.size(); i++) {
            Long s = postList.get(i);
            TBusinessPost businessPost = new TBusinessPost();
            businessPost.setBusinessId(tKs.getId());
            businessPost.setPostId(s + "");
            businessPostService.save(businessPost);
        }
        // 处理部门
        TBusinessDept businessDept = new TBusinessDept();
        businessDept.setBusinessId(tKs.getId());
        List<TBusinessDept> tBusinessDepts = businessDeptService.selectTBusinessDeptList(businessDept);
        for (int i = 0; i < tBusinessDepts.size(); i++) {
            TBusinessDept businessDept1 = tBusinessDepts.get(i);
            businessDeptService.deleteTBusinessDeptById(businessDept1.getId());
        }
        List<Long> deptList = tKs.getDeptList();
        if (StringUtils.isEmpty(deptList)) {
            throw new Exception("请选择部门");
        }
        for (int i = 0; i < deptList.size(); i++) {
            Long s = deptList.get(i);
            TBusinessDept tBusinessDept = new TBusinessDept();
            tBusinessDept.setBusinessId(tKs.getId());
            tBusinessDept.setDeptId(s + "");
            businessDeptService.save(tBusinessDept);
        }

    }


}
